﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SanctusSoftware.Music.Lyrics.Extraction
{
    public static class SongLyricsExtractor
    {

        #region Constructor

        static SongLyricsExtractor()
        {
            providers = new Dictionary<string, ISongLyricsExtractor>();
            providers.Add("[Automatic]", null);
            providers.Add("cyberhymnal.org", new CyberHymnalExtractor());
            providers.Add("hymnsite.org", new HymnSiteExtractor());
        }

        #endregion

        #region Providers

        private static Dictionary<string, ISongLyricsExtractor> providers = null;

        public static void AddProvider(string name, ISongLyricsExtractor provider)
        {
            providers.Add(name, provider);
        }

        public static string[] ProviderNames
        {
            get { return new List<string>(providers.Keys).ToArray(); }
        }

        #endregion

        public static SongLyrics ExtractFromUrl(string url)
        {

            

            return ExtractFromUrl(url, "[Automatic]");


        }

        public static SongLyrics ExtractFromUrl(string url, string provider)
        {

            if (!providers.ContainsKey(provider))
            {
                throw new ArgumentException("Provider '" + provider + "' not found");
            }

            ISongLyricsExtractor sle = null;

            if (provider == "[Automatic]")
            {
                foreach (string name in providers.Keys)
                {
                    if (url.Contains(name))
                    {
                        sle = providers[name];
                        break;
                    }
                }

                if (sle == null)
                {
                    throw new Exception("Failed to automatically determine Song Lyrics Provider for '" + url + "'");
                }
                
            }
            else
            {
                sle = providers[provider];
            }

            SongLyrics sl = sle.ExtractSongFromUrl(url);

            return sl;

        }

    }
}
